package testiranjeSoftvera.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.KeyStroke;

import testiranjeSoftvera.GuiElements.VirusEditorDialog;
import testiranjeSoftvera.phage.PhageCore;


public class ApplyChangesSettingsAction extends AbstractTestAction {

	/**
	 * 
	 */

	private boolean isVerified = false;

	private static final long serialVersionUID = -6702168611457561932L;

	public ApplyChangesSettingsAction() {
		putValue(ACCELERATOR_KEY,
				KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		putValue(javax.swing.Action.NAME, "Apply");
		putValue(SHORT_DESCRIPTION, "Apply");
		putValue(SMALL_ICON, loadIcon("images/filenew.png"));
	}

	public boolean isVerified() {
		return isVerified;
	}

	public void setVerified(boolean isVerified) {
		this.isVerified = isVerified;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (VirusEditorDialog.getInstance().getVirusType().getSelectedItem()
				.equals("Izaberite vas virus")) {
			VirusEditorDialog.getInstance().dispose();
			return;
		}

		doneAction();

	}

	public void doneAction() {

		ArrayList<String> stats = new ArrayList<String>();

		stats.add((VirusEditorDialog.getInstance().getStat1Field().getText()));
		stats.add((VirusEditorDialog.getInstance().getStat2Field().getText()));
		stats.add((VirusEditorDialog.getInstance().getStat3Field().getText()));
		verifyStats(stats);
		if (isVerified) {
			ArrayList<Integer> statsInt = convertStatsToInt(stats);
			

			String nameStr = VirusEditorDialog.getInstance().getVirusType()
					.getSelectedItem().toString();

			switch (nameStr) {
			case "Warrior":
				PhageCore.getInstance().getWarriorWirus()
						.setVirusStats(statsInt);
				System.out.println("Warrior virus  status changed to- "
						+ PhageCore.getInstance().getWarriorWirus()
								.getVirusStats().toString());
				break;
			case "Speedy":
				PhageCore.getInstance().getSpeedyVirus()
						.setVirusStats(statsInt);
				System.out.println("Speedy virus  status changed to- "
						+ PhageCore.getInstance().getSpeedyVirus()
								.getVirusStats().toString());
				break;
			case "Slow":

				PhageCore.getInstance().getSlowVirus()
						.setVirusStats(statsInt);
				System.out.println("SLow virus  status changed to- "
						+ PhageCore.getInstance().getSlowVirus()
								.getVirusStats().toString());
				break;
				
				default:
					break;
			}

			VirusEditorDialog.getInstance().dispose();
		}

		else {
			System.err.println("Nije validan unos podataka ");
			return;
		}

	}

	private ArrayList<Integer> convertStatsToInt(ArrayList<String> stats) {
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		for (int i = 0; i < stats.size(); i++) {

			tmp.add(Integer.parseInt(stats.get(i)));

		}
		return tmp;
	}

	public void verifyStats(ArrayList<String> stats) {
		setVerified(true);
		for (int i = 0; i < stats.size(); i++) {
			String cStat = stats.get(i);
			int b = Character.getNumericValue(cStat.charAt(0));
			if (b <= 0)
				setVerified(false);
			if (cStat.length() != 1
					|| (cStat.length() == 1 && !Character.isDigit(cStat
							.charAt(0)))) {

				setVerified(false);

			}

		}

	}
}
